package com.bibrApp.difu.utils;

import android.text.TextUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class CalendarUtils {
    public static boolean isSameDay(Date date1, Date date2) {
        return formatDateOnly(date1).equals(formatDateOnly(date2));
    }

    public static String formatDateOnly(Date date) {
        try{
            return new SimpleDateFormat("yyyy-MM-dd").format(date);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "";
    }

    public static Date string2Date(String date) {
        try{
            return new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA).parse(date);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public static String formatDate(Date date) {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
    }

    //分钟格式
    public static String formatMinuteDate(Date date) {
        return new SimpleDateFormat("MM-dd HH:mm").format(date);
    }

    public static String formatEntrustDate(String date) {
        if (TextUtils.isEmpty(date)) {
            return "";
        }
        if (date.contains("T")) {
            SimpleDateFormat dff = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.ENGLISH);//输入的被转化的时间格式
            Date date1 = null;
            try {
                date1 = dff.parse(date);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return new SimpleDateFormat("MM-dd HH:mm:ss", Locale.CHINA).format(date1);
        } else {
            Date date1 = new Date(Long.parseLong(date));
            return new SimpleDateFormat("MM-dd HH:mm:ss", Locale.CHINA).format(date1);
        }

    }

    public static String formatLatestDealDate(String date) {
        if (TextUtils.isEmpty(date) || date.contains("--")) {
            return "---";
        }
        SimpleDateFormat dff = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        Date date1 = null;
        try {
            date1 = dff.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return new SimpleDateFormat("MM-dd HH:mm:ss", Locale.CHINA).format(date1);

    }

    /**
     * @param date
     * @return
     * @des 注意加入时区
     */
    public static String formatHisEntrustDate(String date) {
        try {
            if (TextUtils.isEmpty(date)) {
                return "";
            }
            if (date.contains("T")) {
                SimpleDateFormat dff = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.ENGLISH);//输入的被转化的时间格式
                Date date1 = null;
                try {
                    date1 = dff.parse(date);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA).format(date1);
            } else if (date.contains("-")) {
                return date;
            } else {
                Date date1 = new Date(Long.parseLong(date));
                return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA).format(date1);
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return "";
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public static Date stringToDate(String strTime) {
        Long time = Long.parseLong(strTime);
        time = time * 1000;
        Date date = new Date(time);
        return date;
    }

    public static String formatString2Day(String strTime) {
        Long time = Long.parseLong(strTime);
        time = time * 1000;
        Date date = new Date(time);
        return new SimpleDateFormat("yyyy-MM-dd").format(date);
    }

    //10位时间戳
    public static String formatString2Minute(String strTime) {
        Long time = Long.parseLong(strTime);
        time = time * 1000;
        Date date = new Date(time);
        return new SimpleDateFormat("yyyy-MM-dd HH:mm").format(date);
    }

    //10位时间戳
    public static String formatString2Second(String strTime) {
        Long time = Long.parseLong(strTime);
        time = time * 1000;
        Date date = new Date(time);
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
    }

    //13位时间戳
    public static String format13String2Minute(String strTime) {
        Long time = Long.parseLong(strTime);
        Date date = new Date(time);
        return new SimpleDateFormat("MM-dd HH:mm").format(date);
    }

    /**
     * 根据日期获取该日期是该年的多少天
     * @param date
     * @return
     */
    private static Integer getDayNumForYear(Date date){
        Calendar ca = Calendar.getInstance();
        ca.setTime(date);
        return ca.get(Calendar.DAY_OF_YEAR);
    }

    /** 判断传入的日期是否是昨天
     * @param date
     * @return  true为昨天
     */
    public static boolean isYesterday(Date date){
        boolean flag = false;
        // 先获取年份
        int year = Integer.valueOf(new SimpleDateFormat("yyyy").format(date));

        // 获取当前年份 和 一年中的第几天
        int day =  getDayNumForYear(date);
        int currentYear = Integer.valueOf(new SimpleDateFormat("yyyy").format(new Date()));
        Integer currentDay = getDayNumForYear(new Date());
        // 计算 如果是去年的
        if (currentYear - year == 1) {
            // 如果当前正好是 1月1日 计算去年有多少天，指定时间是否是一年中的最后一天
            if (currentDay == 1) {
                int yearDay;
                if (year % 400 == 0) {
                    // 世纪闰年
                    yearDay = 366;
                } else if (year % 4 == 0 && year % 100 != 0) {
                    // 普通闰年
                    yearDay = 366;
                } else {
                    // 平年
                    yearDay = 365;
                }
                if (day == yearDay) {
                    flag = true;
                }
            }
        } else {
            if (currentDay - day == 1) {
                flag = true;
            }
        }
        return flag;
    }

    /**
     * 秒换成00:00
     */

    public static String getCountTimeByLong(long finishTime) {
        int totalTime = (int) (finishTime );//秒
        int minute = 0, second = 0;

        if (60 <= totalTime) {
            minute = totalTime / 60;
            totalTime = totalTime - 60 * minute;
        }
        if (0 <= totalTime) {
            second = totalTime;
        }
        StringBuilder sb = new StringBuilder();

        if (minute < 10) {
            sb.append("0").append(minute).append(":");
        } else {
            sb.append(minute).append(":");
        }
        if (second < 10) {
            sb.append("0").append(second);
        } else {
            sb.append(second);
        }
        return sb.toString();

    }


}